#include<memory>
#include<string>
#include<vector>
#include<cstdio>
#include"err.h"
#include"server.hpp"

using namespace std;
using namespace nt_server;

bool SafeCheck(const string& request)
{
    vector<string> unsafecommand={"kill","mv","rm","while :do","shundown"};

    for(auto& e:unsafecommand)
    {
        if(request.find(e)!=string::npos)
        return false;
    }
    return true;
}

string ExecCommand(const string& request)
{
    //1.安全检查
    if(!SafeCheck(request))
        return "小东西搞事情是吧";

    //2.获取执行结果,打开失败返回NULL
    FILE* fp=popen(request.c_str(),"r");
    if(fp==NULL)
        return "popen Fail";
    //3.将结果读取至字符串中
    std::string ret;
    char buff[1024];
    if(fgets(buff,sizeof(buff),fp)!=NULL)
    {
        ret+=buff;
    }

    //4.关闭文件流
    fclose(fp);
    //5.返回最终执行结果
    return ret;
}




int main()
{
    unique_ptr<UdpServer> userver(new UdpServer(ExecCommand));

    userver->InitServer();

    userver->StartServer();
    return 0;
}